package com.rxyb.hongyu.service.logic;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.dao.ThirdHongyuPushMapper;
import com.rxyb.hongyu.db.po.ThirdHongyuCfg;
import com.rxyb.hongyu.db.po.ThirdHongyuPush;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.dto.HongyuBaseReq;
import com.rxyb.toolize.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author duyh
 * @date 2020/6/9
 */
@Service
public class ThirdHongyuPushLogic {

    @Autowired(required = false)
    private ThirdHongyuPushMapper thirdHongyuPushMapper;

    /**
     * 获取车辆的最近一次访问检车站（成功）
     *
     * @return returnreturn
     * @Param plateType
     * @Param vehiclePlateNo
     **/
//    @Cacheable
    public String getRecentMchnt(String plateType, String vehiclePlateNo) {
        QueryWrapper<ThirdHongyuPush> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdHongyuPush::getVehiclePlateNo, vehiclePlateNo).eq(ThirdHongyuPush::getPlateType, plateType).eq(ThirdHongyuPush::getReqFunc, HongYuConstants.FUNC_20A01).ne(ThirdHongyuPush::getRspState, DbConstants.ROW_STATE_INVALID);
        wrapper.orderByDesc("add_time");
        List<ThirdHongyuPush> selectByExample = thirdHongyuPushMapper.selectList(wrapper);
        if (selectByExample.size() > 0) {
            return selectByExample.get(0).getMchntNo();
        }
        return null;
    }

    public boolean saveHongyu(String reqFunc, ThirdHongyuCfg cfg, HongyuBaseReq hongyuBaseReq, HongYuRsp hongYuRsp) {
        // 重写图片，太长了
        //        JSONObject parseObject = JSONObject.parseObject(reqMsg);
        //        parseObject.put("zp", "");
        //        reqMsg = parseObject.toJSONString();
        ThirdHongyuPush record = new ThirdHongyuPush();
        record.setAddTime(DateTime.getDateTime());
        record.setUpdateTime(DateTime.getDateTime());
        record.setRowState(DbConstants.ROW_STATE_VALID);
        record.setReqFunc(reqFunc);
        record.setReqUrl(cfg.getPostUrl());
        record.setMchntNo(cfg.getMchntNo());
        record.setSerialNum(cfg.getSerialNum());
        record.setRspState(hongYuRsp.getRetcode());
        record.setPlateType(hongyuBaseReq.getHpzl());
        record.setVehiclePlateNo(hongyuBaseReq.getHphm());
        String reqMsg = JSONObject.toJSONString(hongyuBaseReq);
        String rspMsg = JSONObject.toJSONString(hongYuRsp);
        if (reqMsg.length() > 1000) {
            record.setReqMsg(reqMsg.substring(0, 1000));
        } else {
            record.setReqMsg(reqMsg);
        }
        if (rspMsg.length() > 1000) {
            record.setRspMsg(rspMsg.substring(0, 1000));
        } else {
            record.setRspMsg(rspMsg);
        }
        return thirdHongyuPushMapper.insert(record) > 0;
    }

    /**
     * 移除记录
     *
     * @param endTime
     * @return
     */
    public int removeInvalid(Long endTime) {
        QueryWrapper<ThirdHongyuPush> wrapper = new QueryWrapper<>();
        wrapper.lambda().le(ThirdHongyuPush::getAddTime, endTime);
        return thirdHongyuPushMapper.delete(wrapper);
    }
}
